package com.evan.review.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.evan.review.R;

/**
 * DialogFragment演示Activity
 * 展示各种DialogFragment的使用场景
 */
public class DialogFragmentActivity extends AppCompatActivity implements 
        CustomDialogFragment.DialogListener, 
        FullScreenDialogFragment.FullScreenDialogListener {

    private TextView tvLog;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_fragment);
        
        initViews();
    }
    
    private void initViews() {
        tvLog = findViewById(R.id.tv_log);
        
        Button btnAlert = findViewById(R.id.btn_alert_dialog);
        Button btnCustom = findViewById(R.id.btn_custom_dialog);
        Button btnFullscreen = findViewById(R.id.btn_fullscreen_dialog);
        Button btnBottom = findViewById(R.id.btn_bottom_sheet);
        Button btnClearLog = findViewById(R.id.btn_clear_log);
        
        btnAlert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAlertDialog();
            }
        });
        
        btnCustom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCustomDialog();
            }
        });
        
        btnFullscreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showFullScreenDialog();
            }
        });
        
        btnBottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showBottomSheetDialog();
            }
        });
        
        btnClearLog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvLog.setText("");
                appendLog("日志已清空");
            }
        });
    }
    
    /**
     * 显示AlertDialog样式的DialogFragment
     */
    private void showAlertDialog() {
        appendLog("显示AlertDialog样式的DialogFragment");
        AlertDialogFragment dialogFragment = AlertDialogFragment.newInstance(
                "提示", 
                "这是一个基于AlertDialog的DialogFragment示例",
                new AlertDialogFragment.AlertDialogListener() {
                    @Override
                    public void onPositiveClick() {
                        appendLog("AlertDialog: 点击了确定按钮");
                    }
                    
                    @Override
                    public void onNegativeClick() {
                        appendLog("AlertDialog: 点击了取消按钮");
                    }
                });
        
        dialogFragment.show(getSupportFragmentManager(), "alert_dialog");
    }
    
    /**
     * 显示自定义布局的DialogFragment
     */
    private void showCustomDialog() {
        appendLog("显示自定义布局的DialogFragment");
        CustomDialogFragment dialogFragment = CustomDialogFragment.newInstance(
                "这是一条自定义DialogFragment传入的消息");
        dialogFragment.show(getSupportFragmentManager(), "custom_dialog");
    }
    
    /**
     * 显示全屏DialogFragment
     */
    private void showFullScreenDialog() {
        appendLog("显示全屏DialogFragment");
        FullScreenDialogFragment dialogFragment = new FullScreenDialogFragment();
        dialogFragment.show(getSupportFragmentManager(), "fullscreen_dialog");
    }
    
    /**
     * 显示底部弹出的DialogFragment
     */
    private void showBottomSheetDialog() {
        appendLog("显示底部弹出的DialogFragment");
        CustomBottomSheetDialogFragment dialogFragment = new CustomBottomSheetDialogFragment();
        dialogFragment.show(getSupportFragmentManager(), "bottom_sheet_dialog");
    }
    
    @Override
    public void onDialogPositiveClick(CustomDialogFragment dialog, String inputText) {
        appendLog("CustomDialog: 点击了确定按钮，输入内容: " + inputText);
    }
    
    @Override
    public void onDialogNegativeClick(CustomDialogFragment dialog) {
        appendLog("CustomDialog: 点击了取消按钮");
    }
    
    @Override
    public void onFullScreenDialogSave(String result) {
        appendLog("FullScreenDialog: 保存数据: " + result);
    }
    
    @Override
    public void onFullScreenDialogCancel() {
        appendLog("FullScreenDialog: 取消操作");
    }
    
    /**
     * 添加日志
     */
    public void logMessage(String message) {
        appendLog(message);
    }
    
    private void appendLog(String message) {
        String currentLog = tvLog.getText().toString();
        tvLog.setText((currentLog.isEmpty() ? "" : currentLog + "\n") + message);
    }
} 